<?php
	init();
?>

<?php

	function init(){
		if(isset($_POST["SO_id"])){
			$s_today = date('Y-m-d h:i:s A');
			
			$s_subject		= $_POST["txt_subject"];
			$s_description	= htmlentities($_POST["ta_description"],ENT_QUOTES,"UTF-8");
			$i_parent		= $_POST["cb_parent"];
			$s_log			= $s_today." ".$_SESSION["JO_account_id"];
			
			if ($_POST["SO_query"]=="topic_insert"){
				
				$bool_result = topicController::JO_topic_insert($s_subject,$s_description,$i_parent,$s_log);
				
				if($bool_result==1)
					echo "<script>alert('Them Thanh Cong')</script>";
				else 
					echo "<script>alert('Them That Bai')</script>";
				JO_location();
				
			} elseif ($_POST["SO_query"]=="topic_update") {
				
				$i_id = $_POST["SO_id"];
				$bool_result = topicController::JO_topic_update($i_id,$s_subject,$s_description,$i_parent,$s_log);
				
				if($bool_result==1)
					echo "<script>alert('Cap Nhap Thanh Cong')</script>";
				else
					echo "<script>alert('Cap Nhap That Bai')</script>";
				JO_location();
				
			} 
		}elseif (isset($_POST["SO_query"]) && $_POST["SO_query"]=="topic_delete") {
			//echo "<script>alert('Xoa Thanh Cong')</script>";
			$arr_id = $_POST["chk_delete"];
			
			$s_where="";
			foreach ($arr_id as $i_value) {
				$s_where .= " `topic`.`key`=".$i_value." OR";
			}
			
			$s_query="Update `topic` set `delete`=NOW() where".$s_where;
			
			$s_query = substr($s_query, 0, -2);
			
			$bool_result = DataProvider::executeNoneSQL($s_query);
			
			if($bool_result>0)
				echo "<script>alert('Xoa Thanh Cong')</script>";
			else
				echo "<script>alert('Xoa That Bai')</script>";
			JO_location();
				
		}
	}
	
		
	/*
	* ============================================== Business Layer of Topic ======================================
	* Author			: Nguyem Minh Tuan
	* Created Date		: 23/08/2011
	* Modified Date		: 25/08/2011
	*/
		
	class topicController{
		/*
		* Get list of topic with name of parent field
		*/
		public static function JO_topic_getList($s_type,$i_parent){
			$s_SQLCommand = "call JO_topic_getList('".$s_type."',".$i_parent.");";
		
			$rs_result = DataProvider::executeSQL($s_SQLCommand);
		
			return $rs_result;
		}
		
		/*
		 * Get topic with name of parent field
		 */
		public static function JO_topic_get($s_id){
			$s_SQLCommand = "call JO_topic_get(".$s_id.");";
			
			$rs_result = DataProvider::executeSQL($s_SQLCommand);
		
			return $rs_result;
		}
		
		/*
		 * Add a topic
		 */
		public static function JO_topic_insert($s_subject,$s_description,$i_parent,$s_log){
					
			$s_SQLCommand = "call JO_topic_insert(
										". ( $s_subject		? "'".$s_subject."'" 		: "NULL").",
										". ( $s_description	? "'".$s_description."'"	: "NULL").",
										". ( $i_parent		? $i_parent					: 0		).",
										'".$s_log."'
										);";
		
			$rs_result = DataProvider::executeNoneSQL($s_SQLCommand);
			
			//echo $s_SQLCommand;
			
			if($rs_result>0)
				return 1;
			return 0;
		}

		/*
		 * Update a topic
		 */
		public static function JO_topic_update($i_id,$s_subject,$s_description,$i_parent,$s_log){
		
			echo $s_subject;
				
			$s_SQLCommand = "call JO_topic_update(
												". $i_id .",
												". ( $s_subject		? "'".$s_subject."'" 		: "NULL").",
												". ( $s_description	? "'".$s_description."'"	: "NULL").",
												". ( $i_parent		? $i_parent					: 0		).",
												'".$s_log."'
												);";
		
			$rs_result = DataProvider::executeNoneSQL($s_SQLCommand);
				
			//echo $s_SQLCommand;
			if($rs_result>0)
				return 1;
			return 0;
		}
		
		/*
		* Delete a topic
		*/
		public static function JO_topic_delete($i_id,$s_log){
		
			echo $s_subject;
		
			$s_SQLCommand = "call JO_topic_delete( ". $i_id .",'".$s_log."' );";
		
			$rs_result = DataProvider::executeNoneSQL($s_SQLCommand);
		
			//echo $s_SQLCommand;
			if($rs_result>0)
				return 1;
			return 0;
		}
	}
	
?>